package com.kang.until;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.kang.pojo.IntemInformation;

public class InventoryQueryExcelExport extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * xlsx格式
	 * @param request
	 * @param response
	 * @param iinfoList
	 * @return
	 * @throws Exception
	 */
	public static File buildInventoryQueryExcel(HttpServletRequest request, HttpServletResponse response, 
			List<IntemInformation> iinfoList) throws Exception  {
		//请求发送都是utf-8
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
		// 本地年月日时分秒作为文件名
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		File ExcelFile = new File(FileUtil.TEMP_PATH + "库存查询_"+sdf.format(new Date())+".xlsx");
		try {
			// 输出流
			OutputStream os = new FileOutputStream(ExcelFile.getAbsolutePath());
			// 工作区
			XSSFWorkbook wb = new XSSFWorkbook();

			// 工作区名
			XSSFSheet sheet = wb.createSheet("库存查询");

			// 创建表头
			XSSFRow row = sheet.createRow(0);
			row.createCell(0).setCellValue("物品代码");
			row.createCell(1).setCellValue("物品条码");
			row.createCell(2).setCellValue("物品名称");
			row.createCell(3).setCellValue("物品分类");
			row.createCell(4).setCellValue("物料类型");
			row.createCell(5).setCellValue("库存上限");
			row.createCell(6).setCellValue("库存下限");
			row.createCell(7).setCellValue("当前库存");
			row.createCell(8).setCellValue("单位");
			row.createCell(9).setCellValue("物品属性");
			
			// 遍历数据装进table
			for (int i = 0; i < iinfoList.size(); i++) {
				// sheet.createRow(i+1) 因为表头是0
				
				XSSFRow rows = sheet.createRow(i + 1);
				rows.createCell(0).setCellValue(iinfoList.get(i).getItemcode());
				rows.createCell(1).setCellValue(iinfoList.get(i).getArticlebarcode());
				rows.createCell(2).setCellValue(iinfoList.get(i).getChinesename());
				rows.createCell(3).setCellValue(iinfoList.get(i).getCategoryName());
				rows.createCell(4).setCellValue(iinfoList.get(i).getMaterialmaterial());
				rows.createCell(5).setCellValue(iinfoList.get(i).getMaxstock());
				rows.createCell(6).setCellValue(iinfoList.get(i).getMinstock());
				rows.createCell(7).setCellValue(iinfoList.get(i).getCurrentstock());
				rows.createCell(8).setCellValue(iinfoList.get(i).getPurchasingunit());
				rows.createCell(9).setCellValue(iinfoList.get(i).getItemproperties());
				
			}

			// 写文件
			wb.write(os);
			// 关闭输出流
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ExcelFile;
	}
	
	
	/**
	 * xls格式
	 * @param request
	 * @param response
	 * @param iinfoList
	 * @return
	 * @throws Exception
	 */
	/*public static File buildInventoryQueryExcel(HttpServletRequest request, HttpServletResponse response, 
			List<IntemInformation> iinfoList) throws Exception  {
		//请求发送都是utf-8
		request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        
		// 本地年月日时分秒作为文件名
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		File ExcelFile = new File(FileUtil.TEMP_PATH + "库存查询_"+sdf.format(new Date())+".xls");
		try {
			// 输出流
			OutputStream os = new FileOutputStream(ExcelFile.getAbsolutePath());
			// 工作区
			HSSFWorkbook wb = new HSSFWorkbook();

			// 工作区名
			HSSFSheet sheet = wb.createSheet("库存查询");

			// 创建表头
			HSSFRow row = sheet.createRow(0);
			row.createCell(0).setCellValue("物品代码");
			row.createCell(1).setCellValue("物品条码");
			row.createCell(2).setCellValue("物品名称");
			row.createCell(3).setCellValue("物品分类");
			row.createCell(4).setCellValue("物料类型");
			row.createCell(5).setCellValue("库存上限");
			row.createCell(6).setCellValue("库存下限");
			row.createCell(7).setCellValue("当前库存");
			row.createCell(8).setCellValue("单位");
			row.createCell(9).setCellValue("物品属性");
			
			// 遍历数据装进table
			for (int i = 0; i < iinfoList.size(); i++) {
				// sheet.createRow(i+1) 因为表头是0
				
				HSSFRow rows = sheet.createRow(i + 1);
				rows.createCell(0).setCellValue(iinfoList.get(i).getItemcode());
				rows.createCell(1).setCellValue(iinfoList.get(i).getArticlebarcode());
				rows.createCell(2).setCellValue(iinfoList.get(i).getChinesename());
				rows.createCell(3).setCellValue(iinfoList.get(i).getCategoryName());
				rows.createCell(4).setCellValue(iinfoList.get(i).getMaterialmaterial());
				rows.createCell(5).setCellValue(iinfoList.get(i).getMaxstock());
				rows.createCell(6).setCellValue(iinfoList.get(i).getMinstock());
				rows.createCell(7).setCellValue(iinfoList.get(i).getCurrentstock());
				rows.createCell(8).setCellValue(iinfoList.get(i).getPurchasingunit());
				rows.createCell(9).setCellValue(iinfoList.get(i).getItemproperties());
				
			}

			// 写文件
			wb.write(os);
			// 关闭输出流
			os.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ExcelFile;
	}*/

}
